/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.sitesearch.documents;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;

/**
 * Class is responsible for creating singleton instances of XML validators. 
 * Class encapsulates methods for all required validators and returns them on 
 * demand. 
 * 
 * Date of create: Sep 14, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0914
 */
public class Validators {
    
    private static Logger log = Logger.getLogger(Validators.class.getName());
    private static SchemaFactory factory = 
            SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    private static Validator searchResults = null;
    
    /**
     * Method returns singleton instance of validator for the search results.
     * All found results from the page are transformed to XML and then parsed
     * and processed. 
     * 
     * @return singleton instance of validator for search results
     */
    public static Validator getInstanceOfSearchResultValidator(){
        if(searchResults == null){
            try {
                File schemaFile = new File("");
                if(!schemaFile.exists()){
                    log.error("Validation schema file was not found!");
                    return null;
                }
                searchResults = factory.newSchema(schemaFile).newValidator();
            } catch (SAXException ex) {
                log.error("Validator for search results was not created!", ex);
            }
        }
        return searchResults;
    }
    
}
